package com.ysd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysd.dao.impl.CompanyDaoImpl;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.Company;
import com.ysd.entity.User;
import com.ysd.util.Dateutil;
import com.ysd.util.Result;

public class MessageServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CompanyDaoImpl cDao = new CompanyDaoImpl();
		Company c = cDao.getCompany();
		request.setAttribute("c", c);
		
		//int userId = Integer.parseInt(request.getParameter("userId"));
		//request.setCharacterEncoding("UTF-8");设置请求的编码格式为UTF-8
		int userId = 0;
		if(request.getParameter("userId")!=null && !request.getParameter("userId").equals("")) {
			userId = Integer.parseInt(request.getParameter("userId"));
		}
		String username = request.getParameter("username");//接受前台传来的参数
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String content = request.getParameter("content");
		
		
		
		User user = new User(userId, username, phone, email, content);
		response.setCharacterEncoding("UTF-8");//设置响应的编码为UTF-8
		PrintWriter out = response.getWriter();
		UserDaoImpl uDao = new UserDaoImpl();
		int n = uDao.addMessage(user);//受影响的行数
		if (n>0) {//成功
			out.print(Result.toClient(true,"留言成功",""));
		} else {//失败
			out.print(Result.toClient(false,"留言失败","服务器异常"));
		}
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
